/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */

if (!this.bash) bash = {};

bash.version = '0.1';
bash.carret = '$';

bash.user;
bash.currentDir;
bash.rootPath;

bash.current_command=0;
bash.shift=0;
bash.buffer = '';
bash.commandBuffer = new Array();

var callCount=0;
bash.main = function(args)
{
/*
    aFrame = new BFrame("10","10","200","600");

    bashWindow = new BWindow(aFrame,"Terminal",null,null);

    var bframe = new BFrame(0,0,150,600);
    var bView = new BView(bframe,"bView",null,null);    
    bView.addElement("div","content");
    
    cmdRect = new BFrame(0,173,49,600);    
    
    bashWindow.addChild(bView);
    
    cmdLine = new BInput(cmdRect,"commandline",null,benix.stdin);   
    bashWindow.addChild(cmdLine );

    document.getElementById("commandline").value='> '
          
    bashWindow.Show();
*/
    benix.registerStdin(this);
    benix.stdout( 'JS Bash ' + bash.version + '<br>');
    benix.stdout( 'Welcome ' + benix.username + '!<br>');
    document.getElementById("commandline").value= benix.currentDir + ' ' + benix.username + '$ ';
    
};

bash.keydown = function(keycode)
{
    if(keycode == '\\n')
    {
        document.getElementById("commandline").value= benix.currentDir + ' ' + benix.username + '$ ';
        bash.commandBuffer[bash.current_command] = bash.buffer;
        bash.curent_command++; 
        bash.parse(bash.buffer);
        bash.buffer='';        
        return 0;
    }
    else
       bash.buffer = bash.buffer + keycode;    

   return 0;
};

bash.keyup = function(keycode)
{

}

bash.echo = function(arg)
{
    if(arg)
        benix.stdout( arg + '<br>');
    else
        benix.stdout('Bad file Name<br>');
};

bash.mkdir = function(arg)
{
    if(arg && arg[0] == '/')
        benix.write(arg,1,1,null,null,'vfs.sessionKey');
    else
        benix.stdout('Bad file Name<br>');
};

bash.rm = function(arg)
{
    if(arg && arg[0] == '/')
        benix.write(arg,0,1,null,null,'vfs.sessionKey');
     else
        benix.stdout('Bad file Name<br>');       
};

bash.mktxt = function(arg,arg2)
{

    var text="";
    var count=2;
    while(count < arg2.length)
    {
        text= text+arg2[count];
        count++;
    }
    if(arg && arg2)
        benix.write(arg,2,text,null,null,'vfs.sessionKey');
    else
        benix.stdout('Bad file Name<br>');
};

bash.cat = function(arg,arg2)
{
/*
    if(arg && arg2)
    {
        if(arg[0] == '/')
        {
            var buffer3 = benix.read(arg,3,null,null,null,'vfs.sessionKey');
            benix.write(arg2,2,buffer3,null,null,'vfs.sessionKey');
        }
        else
        {
            benix.write(arg2,2,arg,null,null,'vfs.sessionKey');
        }
    }*/
    if(arg)
        benix.stdout( benix.read(arg,3,null,null,null,'vfs.sessionKey') );
    else
        benix.stdout('Bad file Name<br>');   
};

bash.help = function(cmd)
{
    benix.stdout( 'Commands: <br> \
                    cat [<i>input file</i>] [<i>output file</i>]<br>\
                    clear [] <br> \
                    echo [<i>string</i>] <br> \
                    export [<i>string</i>] <br> \
                    help [] <br> \
                    ls [<i>dir</i>] <br> \
                    mkdir [<i>directory</i>]<br>')
};

bash.ls = function(arg)
{
    if(!arg)
        arg = benix.currentDir;

    if(arg.charAt(0)=='/')
    {
        var fileData=0;

        if(benix.read(arg,1,null,null,null,'vfs.sessionKey') )
        {
           readNode =  benix.read(arg,2,null,null,null,'vfs.sessionKey');
           var count = 0;

           nstring = 'ls '+arg+': <br> .<br>..<br>';
           
           if(!readNode.Name)
           {
                benix.stdout(readNode);
                return 0;
            }
           
           if(readNode.Data.length > 0 && readNode.Type == 1)
           {
                while(count <= readNode.Data.length-1)
                {
                    currNode = readNode.Data[count];
                    nstring = nstring+'<br>'+currNode.Name;
                    count++;
                }
                    benix.stdout(nstring + '<br>'); 
            }
            else
            {
                if(readNode.Type != 1)
                     benix.stdout('ls: File not a directory<br>');
                else
                    benix.stdout( nstring + '<br>' );
            }
        }
        else
            benix.stdout('ls: file is not a directory '+fileData + '<br>');
    }
    else
    {
        if(!benix.read( benix.currentDir + '/' + arg,1,null,null,null,'vfs.sessionKey'))
        {
            benix.stdout('ls: not a dir<br>');
            return -1;
        }
        
        readNode =  benix.read( benix.currentDir + '/' + arg,2,null,null,null,'vfs.sessionKey');
            
        readNode =  benix.read( benix.currentDir + '/' + arg,2,null,null,null,'vfs.sessionKey');
           
        if(readNode.Data.length > 0 && readNode.Type == 1)
        {
            while(count <= readNode.Data.length-1)
            {
                    currNode = readNode.Data[count];
                    nstring = nstring+'<br>'+currNode.Name;
                    count++;
            }
                    benix.stdout(nstring + '<br>'); 
            }
            else
            {
                if(readNode.Type != 1)
                    benix.stdout('File not Directory<br>');
                else
                    benix.stdout('.<br>..<br>');
            }
    }

    return 0;
};

bash.parse = function( commandline )
{
    commandlist = commandline.split(" ");

    if( commandlist[0] == 'dump')
    { 
        benix.dumpNode(commandlist[1]);
        return 0;
    }

    if( commandlist[0] == 'tester')
    { 
        btester.main(commandlist[1]);
        return 0;
    }

    if( commandlist[0] == 'cat')
    {
        bash.cat(commandlist[1]);
        return 0;
    }
 
    if( commandlist[0] == 'cp')
    { 
        benix.fsCopy(commandlist[1],commandlist[2],commandlist[3]);
        return 0;
    }
          
    if( commandlist[0] == 'ls')
    {
        bash.ls(commandlist[1]);
        return 0;
    }
    
    if( commandlist[0] == 'mkdir')
    {
        bash.mkdir(commandlist[1]);
        return 0;
    }
    
    if( commandlist[0] == 'mktxt')
    { 
        var buffer = commandline.replace(commandlist[0], "");
        var buffer = buffer.replace(commandlist[1], "");
        
        bash.mktxt(commandlist[1],buffer);
        return 0;
    }

    if( commandlist[0] == 'rm')
    {  
        bash.rm(commandlist[1]);        
        return 0;
    }
    
    if( commandlist[0] == 'mount')
    {
        vfs.mount(commandlist[1],commandlist[2],commandlist[3],
            commandlist[4],commandlist[5],commandlist[6]);
        return 0;
    }
 
    if( commandlist[0] == 'umount')
    {
        vfs.umount(commandlist[1]);
        return 0;
    }
          
    else if( commandlist[0] == 'help')
    {
        bash.help();
        return 0;
    }

    else if( commandlist[0] == 'echo')
    {
        bash.echo(commandlist[1]);
        return 0;
    }

    else if( commandlist[0] == 'export')
    {
        bash.exp(commandlist[1],commandlist[2]);
        return 0;
    }

    else if( commandlist[0] == 'clear')
    {
        benix.flush('stdout');
        return 0;
    }

    else if( commandlist[0] == 'stat')
    {
        vfs.stat(commandlist[1]);
        return 0;
    }
    
    else if( commandlist[0] == 'wget')
    {
        benix.stdout( benix.wget(commandlist[1]) );
        return 0;
    }

    else if( commandlist[0] == 'loadjs')
    {
        jsInclude("http://127.0.0.1/GameKit.js/GameKit.js");
    }

    else if(commandlist[0] == 'gamekit')
    {
        GameKit();
    }

    else if( commandlist[0] == 'image')
    {
        if( commandlist[1] && commandlist[2] )
        {
            buffer = benix.read(commandlist[1],4,null,null,null,6667);
            benix.stdout("<img src=\""+buffer+"\" width=\""+commandlist[2]+"px\" >");
        }
    }
 
    else if( commandlist[0] == 'cd')
    {
        
        if( commandlist[1] == '..')
        {
            benix.currentDir = '/';
            return 0;
        }
        
        if(benix.read(1,benix.currentDir + '/' + commandlist[1]),null,null,null,'vfs.sessionKey' )
        {
            if(benix.currentDir == '/')
            {
                benix.currentDir = '/' + commandlist[1];
                document.getElementById("commandline").value= benix.currentDir + ' ' + benix.username + '$ ';
            }
            else
            {
                benix.currentDir = benix.currentDir + '/' + commandlist[1];
                document.getElementById("commandline").value =  benix.currentDir + ' ' + benix.username + '$ ';
            }
        }
        else
            benix.stdout('cd: ' + commandlist[1] + 'file not directory');
        return 0;
    }
          
    else
    {
        benix.stdout( commandlist[0] + ': command not found<br>' );
    }
};
